/**
 * @Description
 * @author 20155870 杨润增
 * @create 2018-06-10 19:05
 **/
package com.campus_information_portal.service.impl;

import com.campus_information_portal.dao.SelfStudyDao;
import com.campus_information_portal.entity.SelfStudy;
import com.campus_information_portal.entity.Studybyoneself;
import com.campus_information_portal.service.SelfStudyService;
import com.campus_information_portal.util.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @className SelfStudyImpl
 * @Description
 * @author 20155870 杨润增
 * @create 2018-06-10 19:05
 **/
@Service
public class SelfStudyImpl implements SelfStudyService {
    @Autowired
    private SelfStudyDao selfStudyDao;
    @Override
    public Message queryAll() {
        try {
            List<SelfStudy> list=selfStudyDao.queryAll();
            return Message.success().add("list",list);
        }catch (RuntimeException e){
            e.printStackTrace();
        }
        return Message.fail().add("0601","查询失败");
    }

    @Override
    public Message queryByStuId(Integer stuInfoId) {
        try {
            List<SelfStudy> list=selfStudyDao.queryAllByStuId(stuInfoId);
            return Message.success().add("list",list);
        }catch (RuntimeException e){
            e.printStackTrace();
        }
        return Message.fail().add("0602","查询失败");
    }

    @Override
    public Message add(Studybyoneself studybyoneself) {
        try {
            int count=selfStudyDao.count(studybyoneself.getStuInfoId(),studybyoneself.getCourseId());
            System.out.println("+++countcountcount+++"+count);
            if (count<1){
                selfStudyDao.add(studybyoneself);
                return Message.success();
            }else
                return Message.fail().add("0604","你已经申请过了");
        }catch (RuntimeException e){
            e.printStackTrace();
        }
        return Message.fail().add("0603","增加失败");
    }

    @Override
    public Message update(Integer studybyoneselfFlag, Integer studybyoneselfId) {
        try {
            selfStudyDao.updateFlag(studybyoneselfFlag,studybyoneselfId);
            return Message.fail().add("0606","修改成功");
        }catch (RuntimeException e){
            e.printStackTrace();
        }
        return Message.fail().add("0605","修改失败");
    }
}
